<%@ page import="java.io.FileInputStream" %>
<%--
  Created by IntelliJ IDEA.
  User: Fan307
  Date: 2016/11/1
  Time: 15:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="application/x-download;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>文档显示</title>
</head>
<body >
    <%
        //采用二进制流下载，对于文件夹中的取名，一定不能有空格，在后期上传文件时一定要判断上传的文件名不能带有空格

        //路径拼接参照displyPdfPc.jsp中的方法
        String filepath = request.getParameter("filepath").substring(3);
        //得到对应文档的绝对路径
        String strFilePath = application.getRealPath("/") + filepath;
        //取出所选文件的名字，带有后缀
        String filename1 = filepath.substring(filepath.lastIndexOf('\\')+1);
        System.out.println(filename1 + " : "  + strFilePath);
        filename1 = new String(filename1.getBytes("gb2312"), "ISO8859-1" );

        int status=0;
        byte b[]=new byte[1024];
        FileInputStream in=null;
        ServletOutputStream out2=null;
        try
        {
//            response.setHeader("content-disposition","attachment; filename=d.zip");
//            in=new FileInputStream("c:\\tomcat\\webapps\\ROOT\\d.zip");
            response.setHeader("content-disposition","attachment; filename=" + filename1);
            in=new FileInputStream(strFilePath);
            out2=response.getOutputStream();
            while(status != -1 )
            {
                status=in.read(b);
                out2.write(b);
            }
            out2.flush();
        }
        catch(Exception e)
        {
            System.out.println(e);
            response.sendRedirect("/Pages/downError.jsp");
        }
        finally
        {
            if(in!=null)
                in.close();
            if(out2 !=null)
                out2.close();
        }
    %>
    <%
        //response.setHeader("Content-disposition","inline; filename=D:\\IdeaProjects\\Education\\out\\artifacts\\Education_war_exploded\\res\\computer\\CDUT\\program\\C\\请审批表.doc ");
    %>
</body>
</html>
